package com.apps.ftapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.SeekBar;

import com.apps.ftapp.R;
import com.apps.ftapp.util.Constant;
import com.apps.ftapp.util.L;
import com.duobeiyun.bean.ChatBean;
import com.duobeiyun.callback.PlaybackMessageCallback;
import com.duobeiyun.player.PlaybackPlayer;
import com.duobeiyun.util.EncryptUtils;
import com.duobeiyun.widget.PlaybackPlayerView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class DbHuifangActivity extends BaseActivity implements PlaybackMessageCallback {

    @BindView(R.id.playbackPlayer)
    PlaybackPlayerView playbackPlayerView;
    @BindView(R.id.mSeekBar)
    SeekBar mSeekBar;

    String timestamp = String.valueOf(System.currentTimeMillis());
    String baseUrl = "http://api.duobeiyun.com/api/v3/room/enter?";
    String appKey;
    String partner;
    String uid;
    String sign;
    String url;
    String userRole = "2";
    private String title;

    private String roomId;
    private String nickname;

    private PlaybackPlayer playbackPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_db_huifang);
        ButterKnife.bind(this);

//        setTitle("1111");

        Intent intent = this.getIntent();
        Bundle bundle = this.getIntent().getExtras();
        roomId = bundle.getString("roomId");
        nickname = bundle.getString("nickname");

        setTitle("" + bundle.getString("title"));
        L.i("main", ":========:::" + roomId + "::::" + nickname);

        if (intent != null) {
            String purl = intent.getStringExtra("open_url");
            if (purl != null) {
                this.url = purl;
            } else {
                this.appKey = intent.getStringExtra("appKey") != null ? intent.getStringExtra("appKey") : "";
                this.nickname = intent.getStringExtra("nickname") != null ? intent.getStringExtra("nickname") : "";
                this.partner = intent.getStringExtra("partner") != null ? intent.getStringExtra("partner") : "";
                this.roomId = intent.getStringExtra("roomId") != null ? intent.getStringExtra("roomId") : "";
                this.uid = intent.getStringExtra("uid") != null ? intent.getStringExtra("uid") : "";
                this.title = intent.getStringExtra("title") != null ? intent.getStringExtra("title") : "在线回放";
                this.generateUrl();
            }
        } else {
            this.url = "http://www.duobeiyun.com";
        }


        initPlayer();


    }


    public void generateUrl() {
        StringBuffer sb = new StringBuffer();
        sb.append("nickname=").append(this.nickname).append("&").append("partner=").append(this.partner).append("&").append("roomId=").append(this.roomId).append("&").append("timestamp=").append(this.timestamp).append("&").append("uid=").append(this.uid).append("&").append("userRole=").append(this.userRole);
        String pMD5Str = sb.toString() + this.appKey;
        this.sign = EncryptUtils.MD5(pMD5Str);
        this.url = this.baseUrl + sb.append("&").append("sign=").append(this.sign).toString();
        L.i("main", this.url);
    }


    //初始化播放器
    public void initPlayer() {

        playbackPlayer=new PlaybackPlayer(this.getApplicationContext(),playbackPlayerView);

        playbackPlayer.authInit(Constant.PID, Constant.APPKEY);//pid,appkey和直播使用的是一样的
        //videoTeacher,videoStudent；老师和学生的视频窗口
        playbackPlayer.setVideoView(null, null);
        //initplayer传入回放相关的数据；也可以通过后台传递url;
//        playbackPlayer.initPlayInfo(Constant.uid, Constant.nickname, roomId, 0,
//                RoleType.ROLE_TYPE_STUDENT, 30000, this);//本地创建相应的属性进 教室，测试使
        playbackPlayer.initPlayInfo(url, roomId, 1, 3000, this);
//        playbackPlayer.setSpeedPlay(speed)//控制播放速度 playbackPlayer.startPlayback();//开始回放播放 playbackPlayer.pause();//暂停回放播放
//        jroomId,playMod,timeOut,this);
//        playbackPlayer.initPlayInfo(Constant.uid, Constant.nickname, roomId, 0,
//                RoleType.ROLE_TYPE_STUDENT, 1000, this);
        //设置横竖屏的进度条
        playbackPlayer.setSeekBar(mSeekBar);
        playbackPlayer.setLanSeekBar(mSeekBar);
        playbackPlayer.startPlayback();

    }


    @Override
    protected void onStart() {
        super.onStart();
        if (playbackPlayer != null) {
//            playbackPlayer.startPlayback();
        }
    }

    @Override
    protected void onPause() {
        if (playbackPlayer != null) {
            playbackPlayer.pause();
        }
        super.onPause();
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (playbackPlayer != null) {
            playbackPlayer.release();
        }
    }


    @Override
    public void loadStart() {

    }

    @Override
    public void handleContent(ChatBean chatBean) {

    }

    @Override
    public void handleContent(ArrayList<ChatBean> arrayList) {

    }

    @Override
    public void networkNotConnected() {

    }

    @Override
    public void handleClearChat() {

    }

    @Override
    public void statusCode(int i, String s) {

    }

    @Override
    public void kickoff() {

    }

    @Override
    public void pptMessage(int i, int i1) {

    }

    @Override
    public void connected() {

    }

    @Override
    public void connectFail(String s) {

    }

//    @Override
//    public void loadFinish() {
//
//    }
//
//    @Override
//    public void loadFail(String s) {
//
//    }
//
//    @Override
//    public void connectTimeout() {
//
//    }

//    @Override
//    public void statusCode(int i) {
//        L.i("main", "状态码:" + i);
//    }
//
//    @Override
//    public void exit() {
//
//    }

    @Override
    public void getTotalTime(String s) {

    }

//    @Override
//    public void getPPTinfo(int i, int i1) {
//
//    }

    @Override
    public void currentTime(String s) {

    }

    @Override
    public void playPuase(boolean b) {

    }

    @Override
    public void playFinish() {

    }

//    @Override
//    public void playPuase() {
//
//    }
}
